/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddStorageItem.java
 *
 * Created on 16-05-2011, 12:45:09
 */
package views;

import controllers.StorageController;
import controllers.StorageItemController;
import db.StorageItemDb;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import models.*;

/**
 *
 * @author bruger
 */
public class AddStorageItem extends AbstractView {

    private ArrayList<Storage> storageList = new ArrayList<Storage>();
    private StorageController stoCtr = new StorageController();
    private StorageItemController stoItemCtr = new StorageItemController();
    private StorageItemDb stoItemDb = new StorageItemDb();
    private ArrayList<StorageItem> storageItemList = new ArrayList<StorageItem>();

    /** Creates new form AddStorageItem */
    public AddStorageItem() {
        initComponents();
        getListsItem();
    }

    public void getListsItem() {
        DefaultListModel itemlistModel = new DefaultListModel();
        storageList = stoCtr.getAllStorage();
        for (Storage s : storageList) {
            itemlistModel.addElement("#" + s.getId() + ": " + s.getName() + "     " + s.getAmount() + "stk." + "     Antal eksemplar" + stoItemCtr.getExsempelAmount(s.getId()));
        }
        listItems.removeAll();
        listItems.setModel(itemlistModel);


        DefaultListModel rentlistModel = new DefaultListModel();
        storageItemList = stoItemCtr.getAllStorageItem();

        for (StorageItem si : storageItemList) {
            rentlistModel.addElement("#" + si.getSerialnumber() + ": " + si.getStorageName() + "     Forsvund" + si.getLostAmount() + "     " + si.getAmount() + "stk.");
        }
        listRentItem.removeAll();
        listRentItem.setModel(rentlistModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listItems = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCopyAmount = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtSerial = new javax.swing.JTextField();
        btnAddItem = new javax.swing.JButton();
        lblStatus = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listRentItem = new javax.swing.JList();
        btnDelete = new javax.swing.JButton();
        lblStatusRent = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tilføj eksemplar"));
        jPanel1.setAutoscrolls(true);
        jPanel1.setFocusable(false);

        jScrollPane1.setViewportView(listItems);

        jLabel1.setText("Ting på lager:");

        jLabel2.setText("Antal eksemplar:");

        jLabel3.setText("Serienummer:");

        btnAddItem.setText("Tilføj");
        btnAddItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddItemActionPerformed(evt);
            }
        });

        lblStatus.setText("Status:");

        jLabel4.setText("Vælge den ting du vil oprette flere eksemplar af til udlån.");

        jLabel5.setText("Hvor mange er der f.eks. hvis det er en kasse trøjer.");

        jLabel6.setText("Hvis antal er over 1 får de samme serienummer.");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtSerial)
                                .addComponent(txtCopyAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel5))))
                    .addComponent(btnAddItem, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCopyAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtSerial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAddItem)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Ting der kan udlånes"));

        jScrollPane2.setViewportView(listRentItem);

        btnDelete.setText("Slet");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        lblStatusRent.setText("Status:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)
                            .addComponent(btnDelete))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblStatusRent, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                        .addGap(89, 89, 89))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblStatusRent)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnDelete)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
        int itemId = listItems.getSelectedIndex();
        if (itemId > -1) {
            Storage stoObj = storageList.get(itemId);
            try {
                int copyAmount = Integer.valueOf(txtCopyAmount.getText());
                String serial = txtSerial.getText();
                if (stoItemCtr.getExsempelAmount(stoObj.getId()) + copyAmount < stoObj.getAmount()) {
                    try {
                        StorageItem item = stoItemCtr.buildItem(stoObj.getId(), copyAmount, 0, serial);
                  // FEJL!      stoItemDb.addIteam(item);
                        stoObj.addItemCoby(item);
                        lblStatus.setText("Status: Eksemplarne blev oprettet");
                        storageItemList.add(item);
                        updateFields();
                    } catch (Exception e) {
                        lblStatus.setText("Status: Eksemplarne blev ikke oprettet!");
                    }
                } else {
                    lblStatus.setText("Status: Du prøver at lave flere der kan udlånes ind der er på lager, ændre antallet!");
                }
            } catch (Exception e) {
                lblStatus.setText("Status: Du har ikke skrevet antal!");
            }
        } else {
            lblStatus.setText("Status: Du har ikke valgt en ting!");
        }

    }//GEN-LAST:event_btnAddItemActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        int rentItemId = listRentItem.getSelectedIndex();
        if (rentItemId > -1) {
            stoItemDb.deletedItem(rentItemId);
            lblStatus.setText("Tingen/tingene er blevet slettet og kan ikke længere udlånes!");
            storageItemList.remove(rentItemId);
            updateFields();
        } else {
            lblStatusRent.setText("Der skete en fejl");
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void updateFields() {

        DefaultListModel itemlistModel = new DefaultListModel();
        for (StorageItem si : this.storageItemList) {
            itemlistModel.addElement("#" + si.getSerialnumber() + ": " + si.getStorageName() + "     Forsvund" + si.getLostAmount() + "     " + si.getAmount() + "stk.");
        }
        listRentItem.removeAll();
        listRentItem.setModel(itemlistModel);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AddStorageItem().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddItem;
    private javax.swing.JButton btnDelete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblStatusRent;
    private javax.swing.JList listItems;
    private javax.swing.JList listRentItem;
    private javax.swing.JTextField txtCopyAmount;
    private javax.swing.JTextField txtSerial;
    // End of variables declaration//GEN-END:variables
}
